//
//  DYMSWentiView.swift
//  
//
//  Created by 叁拾叁 on 2021/2/6.
//


import UIKit

///  便签view
class DYMSWentiView: UIView {
    
    public var clickClosure: ((Int)->Void)?
    /// 总宽度
    public var width: CGFloat = SCREEN_WIDTH - 24
    
    /// 便签区域上下距离父视图的距离
    public var topMargin: CGFloat = 0
    
    /// 便签区域左右距离父视图的距离
    public var leftMargin: CGFloat = 15
    
    /// 便签高度
    public var height: CGFloat = 25
    
    /// 字体大小 在setupBtns方法之前传入用于计算便签width
    public var font: UIFont = UIFont.systemFont(ofSize: 12)
    
    /// 便签之间的左右间距
    public var marginWidth: CGFloat = 10
    
    /// 便签之间的上下间距
    public var marginHeight: CGFloat = 10
    
    /// 文字距离左右边框的距离
    public var textMargin: CGFloat = 6
    
    /// 文字颜色
    public var textColor: UIColor = UIColor(hex: 0x3E3E3E)
    
    /// 边框颜色
    public var borderColor: UIColor = UIColor(hex: 0x1797CE)

    /// 边框
    public var borderWidth: CGFloat = 0

    /// 背景颜色
    public var bgColor: UIColor = UIColor(hex: 0xF4F4F4)

    override init(frame: CGRect) {
        super.init(frame: frame)
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    
        //MARK:- 获取标签的高度
    public class func getViewHeight(texts:[String],leftMargin:CGFloat = 12,topMargin:CGFloat = 0,font:CGFloat = 10,width:CGFloat = SCREEN_WIDTH - 24,height:CGFloat = 16,textMargin:CGFloat = 6,marginHeight:CGFloat = 4,marginWidth:CGFloat = 4) -> CGFloat {
        var tagBtnX: CGFloat = leftMargin
        var tagBtnY: CGFloat = topMargin
        
        for (_, value) in texts.enumerated() {
            let tagTextSize: CGSize = value.getSize(font: UIFont.systemFont(ofSize: font), maxSize: CGSize(width: width, height: height))
            if tagBtnX + tagTextSize.width + textMargin * 2 > SCREEN_WIDTH - leftMargin * 2 {
                tagBtnX = leftMargin
                tagBtnY += height + marginHeight
            }
            tagBtnX = tagBtnX + tagTextSize.width + textMargin * 2 + marginWidth // 间距
        }
        return tagBtnY + height + topMargin
    }
    
    public func initViews(texts: [String], singlelLine: Bool = false) -> CGFloat {
        
        removeBtns()
        if texts.count == 0 { return 0 }
        var tagBtnX: CGFloat = leftMargin
        var tagBtnY: CGFloat = topMargin
        for (i, value) in texts.enumerated() {
            let tagTextSize: CGSize = value.getSize(font: self.font, maxSize: CGSize(width: width, height: height))
            if tagBtnX + tagTextSize.width + textMargin * 2 > self.width - leftMargin * 2 {
                if !singlelLine {
                    tagBtnX = leftMargin
                    tagBtnY += height + marginHeight
                } else {
                    break
                }
            }
            let tagBtn: UIButton = UIButton(frame: CGRect(x: tagBtnX, y: tagBtnY, width: tagTextSize.width + textMargin * 2, height: height))
            tagBtn.setTitle(value, for: .normal)
            tagBtn.setTitleColor(textColor, for: .normal)
            tagBtn.setTitleColor(UIColor.white, for: .selected)
            tagBtn.titleLabel?.font = self.font
            tagBtn.layer.cornerRadius = 4
            tagBtn.layer.masksToBounds = true
            tagBtn.layer.borderWidth = borderWidth
            tagBtn.layer.borderColor = borderColor.cgColor
            tagBtn.backgroundColor = bgColor
            tagBtn.addTarget(self, action: #selector(tagBtnAction), for: .touchUpInside)
            tagBtn.tag = i + 100
            self.addSubview(tagBtn)
            tagBtnX = tagBtn.frame.maxX + marginWidth // 间距
        }
        return tagBtnY + height + topMargin
    }
    
    public func removeBtns() {
        if self.subviews.count > 0 {
            let _ = self.subviews.map { $0.removeFromSuperview() }
        }
    }

    @objc func tagBtnAction(sender: UIButton) {
        print(sender.tag)
        sender.backgroundColor = textColor
        sender.isSelected = true
        clickClosure?(sender.tag-100)
    }

}
